<?php 
namespace app\api\model;
use app\common\model\Region as RegionModel;
/**
 * 
 */
class LbsModel
{
	/**
	 * 腾讯api
	 */
	public static function lbsApi($lat,$lng)
	{
		$sig = self::getSig($lat,$lng);
		$url = "https://apis.map.qq.com/ws/geocoder/v1?key=SBZBZ-ABJRG-YZUQR-IOHVO-RHHYS-EHFU6&location=".$lat.",".$lng."&sig=".$sig;
		$city = self::getData($url);
		if (!$city) {
			return [];
		}
		return static::getCity($city);
	}

	/**
	 * 签名
	 */
	public static function getSig($lat,$lng)
	{
		return md5("/ws/geocoder/v1?key=SBZBZ-ABJRG-YZUQR-IOHVO-RHHYS-EHFU6&location=".$lat.",".$lng."gbcWO2RHco6lXBKbRrSDovIYY61LiTu");
	}
	
	/**
	 * 处理数据
	 */
	public static function getData($url)
	{
		$data = curl($url);
		if (!empty($data)) {
			$address = json_decode($data);
			if (isset($address->status) && $address->status == 0) {
				$result = $address->result;
				$ad_info = $result->ad_info;
				return $ad_info->city;
			}else{
				return false;
			}
		}else{
			return false;
		}
	}

	/**
	 * 数据库查询
	 */
	public static function getCity($city)
	{
        $where['name'] = ['like', $city];
        $where['level'] = 2;
        $model = RegionModel::useGlobalScope(false)
                ->field('id, pid, name, level, first, zip_code, is_public')
                ->where($where)
                ->find();
         return $model;
	}

}
?>